/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import supermarketv0_class.*;

/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class Relation_StoreProduct extends javax.swing.JFrame {

    SuperMarketMainView1 mainscreen;
    Store store;
    List stores;
    Product product;
    List products;
    ArrayList<Store> st = new ArrayList();
    ArrayList<Product> pr = new ArrayList();
    List<Product> p;
    private DefaultListModel categModel1, categModel2, categModel3;//model ΠΟΥ ΚΟΥΜΠΩΝΕΙ ΠΑΝΩΣΤΗ ΛΙΣΤΑ

    /**
     * Creates new form Relation_StoreProduct
     */
    public Relation_StoreProduct(SuperMarketMainView1 mainscreen, Store store, Product product) {
       initComponents();
        this.mainscreen = mainscreen;
        this.store = store;
        this.product = product; 
       
        stores = mainscreen.db.readStores();//ανάγνωση Καταστημάτων & Προϊοντων
        products = mainscreen.db.readProduct();
        
        jComboBox1.removeAllItems();//ΚΑΘΑΡΙΣΜΟΣ
        jComboBox1.addItem("Select Store");
        Iterator itr = stores.iterator();
        while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗ ΛΙΣΤΑ
            Store s = (Store) itr.next();
            jComboBox1.addItem(s);//ΠΡΟΣΘΗΚΗ ΣΤΟ  combobox
          
        }
        
       
        jList1.removeAll();//ΑΔΙΑΣΜΑ ΛΙΣΤΑΣ
        categModel1 = new DefaultListModel();//ΝΕΟ ΜΟΝΤΕΛΟ
        categModel2 = new DefaultListModel();//ΝΕΟ ΜΟΝΤΕΛΟ
        itr = products.iterator();//ΔΕΙΚΤΗΣ ΚΑΤΗΓΟΡΙΑ
        while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗΝ ΛΙΣΤΑ
            Product p = (Product) itr.next();//ΠΙΑΝΩ το Προϊόν
            categModel1.addElement(p);//ΠΡΟΣΘΗΚΗ ΣΤΗ ΛΙΣΤΑ
        }
        jList1.setModel(categModel1);//ΚΟΥΜΠΩΝΩ ΤΟ ΜΟΝΤΕΛΟ ΣΤΗ ΛΙΣΤΑ

      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(Relation_StoreProduct.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList2.setName("jList2"); // NOI18N
        jScrollPane2.setViewportView(jList2);

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton3)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(55, 55, 55)
                                            .addComponent(jLabel3))
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(179, 179, 179)))
                        .addGap(50, 50, 50)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(47, 47, 47))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton2)
                                    .addComponent(jButton1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(46, 46, 46)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(51, 51, 51))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4)
                            .addComponent(jButton3))))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here: Προσθήκη -->
        st.clear();
        if (jComboBox1.getSelectedIndex() > 0) {//αν έχω επιλέξει κατάστημα
            store = (Store) jComboBox1.getSelectedItem();
            jLabel6.setText(store.getName());
            st.add(store);
            
            while (jList1.getSelectedIndex() >= 0) {//αν επέλεξα κάτι από τα προϊόντα
                product = (Product) jList1.getSelectedValue();//πάρε το
                jLabel7.setText(product.getName());
                categModel2.addElement(product);//προσθήκη στη δεύτερη λίστα
                jList2.setModel(categModel2);
                pr.add(product);
                categModel1.removeElement(product); //αφαίρεση από πρώτη λίστα
                jList1.setModel(categModel1);

            }
            

        }


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here: Αφαίρεση <--
        st.clear();
        if (jComboBox1.getSelectedIndex() > 0) {//αν έχω επιλέξει κατάστημα
            store = (Store) jComboBox1.getSelectedItem();
            jLabel6.setText(store.getName());
            st.add(store);
            while (jList2.getSelectedIndex() >= 0) {//αν επέλεξα κάτι από τα προϊόντα
                product = (Product) jList2.getSelectedValue();//πάρε το 
                jLabel7.setText(product.getName());
                categModel2.removeElement(product);//αφαίρεση από δεύτερη λίστα
                jList2.setModel(categModel2);

                pr.remove(product);

                categModel1.addElement(product);
                categModel3 = categModel1;
                jList1.setModel(categModel1);
            }

        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:Αποθήκευση<<με μεθοδο>>
        mainscreen.db.RelationStoreProduct(st, pr);

        dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:Ακύρωση
        st.clear();
        pr.clear();
        categModel2.removeAllElements();
        jList2.setModel(categModel2);
        jList1.removeAll();
        categModel1 = new DefaultListModel();
        Iterator itr = products.iterator();//ΔΕΙΚΤΗΣ ΚΑΤΗΓΟΡΙΑ
        while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗΝ ΛΙΣΤΑ
            Product p = (Product) itr.next();//ΠΙΑΝΩ το Προϊόν
            categModel1.addElement(p);//ΠΡΟΣΘΗΚΗ ΣΤΗ ΛΙΣΤΑ
        }
        jList1.setModel(categModel1);//ΚΟΥΜΠΩΝΩ ΤΟ ΜΟΝΤΕΛΟ ΣΤΗ ΛΙΣΤΑ
        //  mainscreen.db.RelationStoreProduct(s, p);
        dispose();

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:

        if (jComboBox1.getSelectedIndex() > 0) { //αν έχω επιλέξει κατάστημα
            jList1.removeAll();
            categModel1.clear();
            Iterator itr = products.iterator();//ΔΕΙΚΤΗΣ ΚΑΤΗΓΟΡΙΑ
            while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗΝ ΛΙΣΤΑ
                Product p = (Product) itr.next();//ΠΙΑΝΩ το Προϊόν
                categModel1.addElement(p);//ΠΡΟΣΘΗΚΗ ΣΤΗ ΛΙΣΤΑ
            }
            jList1.setModel(categModel1);//ΚΟΥΜΠΩΝΩ ΤΟ ΜΟΝΤΕΛΟ ΣΤΗ ΛΙΣΤΑ

            jList2.removeAll();
            categModel2.clear();
           //  jComboBox1.removeAllItems();
            Store s = (Store) jComboBox1.getSelectedItem();
            jLabel6.setText(s.getName());
            itr = products.iterator();//ΔΕΙΚΤΗΣ ΚΑΤΗΓΟΡΙΑ
            while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗΝ ΛΙΣΤΑ
                Product p = (Product) itr.next();//ΠΙΑΝΩ το Προϊόν
                Iterator itr_store = p.getStoreList().iterator();
                while (itr_store.hasNext()) {
                    Store s1 = (Store) itr_store.next();
                      
                    if (s1.getStoreId() == s.getStoreId()) {
                        categModel2.addElement(p);//ΠΡΟΣΘΗΚΗ ΣΤΗ ΛΙΣΤΑ
                        categModel1.removeElement(p);
                        jList1.setModel(categModel1);//ΚΟΥΜΠΩΝΩ ΤΟ ΜΟΝΤΕΛΟ ΣΤΗ ΛΙΣΤΑ
                        jList2.setModel(categModel2);//ΚΟΥΜΠΩΝΩ ΤΟ ΜΟΝΤΕΛΟ ΣΤΗ ΛΙΣΤΑ
                    }
                }
            }
        }

    }//GEN-LAST:event_jComboBox1ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
